package org.csu.mypetstore.api.controller.front;


import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.common.ResponseCode;
import org.csu.mypetstore.api.entity.Account;
import org.csu.mypetstore.api.entity.Profile;
import org.csu.mypetstore.api.entity.SignOn;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/account/")
public class AccountController {

      @Autowired
      private AccountService accountService;


      @PostMapping("login")
      @ResponseBody
      public CommonResponse<AccountVO> login(@RequestParam String username, @RequestParam String password, HttpSession session){
            CommonResponse<AccountVO> response=accountService.getAccount(username,password);
            if(response.isSuccess()){
                  session.setAttribute("login_account",response.getData());

                  session.setAttribute("username",response.getData().getUsername());//
            }
            return response;
      }

      @PostMapping("logout")
      @ResponseBody
      public  CommonResponse<String> logout(HttpSession session){
            AccountVO loginAcount=(AccountVO) session.getAttribute("login_account");
            if(loginAcount !=null){
                  session.removeAttribute("login_account");
                  return CommonResponse.createForSuccessMessage("成功退出登录");
            }
            return CommonResponse.createForError("用户未登录，不能退出");
//            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，不能获取用户信息");
      }


      @PostMapping("get_login_account_info")
      @ResponseBody
      public  CommonResponse<AccountVO> getLoginAccountInfo(HttpSession session){
            AccountVO loginAcount=(AccountVO) session.getAttribute("login_account");
            if(loginAcount !=null){
                  return  CommonResponse.createForSuccess(loginAcount);
            }
            return CommonResponse.createForError("用户未登录，不能获取信息");
//            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，不能获取用户信息");
      }

      @PostMapping("accounts")
      @ResponseBody
      public CommonResponse<AccountVO> register(Account account, @RequestParam("password")String password,@RequestParam("language") String langpref,@RequestParam("favcategory")String favcategory,HttpSession session){
            System.out.println("******");
            CommonResponse<AccountVO> response=accountService.registerAccount(account,password,langpref,favcategory);
            if(response.isSuccess()){
                  session.setAttribute("login_account",response.getData());

                  session.setAttribute("username",response.getData().getUsername());
            }
            return response;
      }

      @PutMapping("accounts/{id}")
      @ResponseBody
      public CommonResponse<AccountVO> editAccount(@PathVariable("id") String username, Account account,@RequestParam("signOn") SignOn signOn,
                                                   @RequestParam("profile") Profile profile, @RequestParam("password")String password, HttpSession session){

            CommonResponse<AccountVO> response=accountService.editAccount(username,account,signOn,profile);

            return response;
      }

}
